/*
   Copyright (c) 2020, zoomdy
   MicroAmpereII is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2.
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
   EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
   MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
   See the Mulan PSL v2 for more details.
 */
#include "pt/pt.h"
#include "hal_cdc.h"
#include "app_cdc.h"

typedef struct
{
    struct pt pt;
    hal_cdc_req_t req;
    uint8_t buffer[HAL_CDC_PACKET_SIZE];
}app_cdc_t;

static app_cdc_t app_cdc;


void app_cdc_init(void)
{
    app_cdc_t* self = &app_cdc;

    PT_INIT(&self->pt);
}


PT_THREAD(app_cdc_task(void))
{
    app_cdc_t* self = &app_cdc;

    PT_BEGIN(&self->pt);

    while(1) {
#if 0
        self->req.buffer = self->buffer;
        self->req.length = sizeof(self->buffer);

        hal_cdc_recv(&self->req);

        PT_WAIT_UNTIL(&self->pt, hal_cdc_error(&self->req) != HAL_ERR_INPROGRESS);

        hal_cdc_send(&self->req);

        PT_WAIT_UNTIL(&self->pt, hal_cdc_error(&self->req) != HAL_ERR_INPROGRESS);
#else

        PT_YIELD(&self->pt);
#endif
    }

    PT_END(&self->pt);
}
